const multer = require("@koa/multer");
const { UPLOAD_FILE_PATH } = require("../config/path");

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, `${UPLOAD_FILE_PATH}`);
    },
    filename: function (req, file, cb) {
        cb(null, `${Date.now()}_${Buffer.from(file.originalname, "binary").toString()}`);
    },
});

// 上传文件中间件
const uploadFile = multer({
    storage,
});

// 头像上传中间件
const multerUploadFile = uploadFile.single("file");

module.exports = {
    multerUploadFile,
};
